package com.coders.temperatureconverter;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class TemperatureconverterActivity extends Activity {
    /** Called when the activity is first created. */
	private EditText text;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //EditText text = (EditText) findViewById(R.id.editText1);
       
        text = (EditText) findViewById(R.id.editText1);
    }
    
    
    public void myClickHandler(View view)
    {
    	switch(view.getId())
    	{
    		case R.id.button1:
    		RadioButton celsiusButton = (RadioButton) findViewById(R.id.radioButton1);
    		RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radioButton2);
    		EditText text = (EditText) findViewById(R.id.editText1);
    		if (text.getText().length() == 0)
    		{
    			Toast.makeText(this, "Please enter a valid number",Toast.LENGTH_LONG).show();
				return;
    			
    		}
    		
    		float inputValue = Float.parseFloat(text.getText().toString());
    		
    		if(celsiusButton.isChecked())
    		{
    		
    			text.setText(String.valueOf(convertFToC(inputValue)));
    			
    			celsiusButton.setChecked(false);
				fahrenheitButton.setChecked(true);
    		}
    		else
    		{
    			text.setText(String.valueOf(convertFToC(inputValue)));
    			celsiusButton.setChecked(true);
				fahrenheitButton.setChecked(false);
    		}
    		break;
    		
    		
    	}
    }

    private float convertFToC(float fahrenheit) {
		return ((fahrenheit - 32) * 5 / 9);
	}

	// Converts to fahrenheit
	private float convertCToF(float celsius) {
		return ((celsius * 9) / 5) + 32;
	}


}

